package com.jackrain.nea.omslocal.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.omslocal.conf.LoadCloumnsConfig;
import com.jackrain.nea.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Discription: 调用线上接口, 获取系统表字段数据
 * Created with IntelliJ IDEA.
 * @Author cs
 * @Date 2017/5/24 17:03
 */
@Component
@Slf4j
public class GetTableCloumnsService {

    @Autowired
    private LoadCloumnsConfig loadCloumnsConfig;

    @Autowired
    private PropertiesConf pconf;

    public int getTableCloumns() {


        try {
            String resultStr = HttpUtil.httpPostRequest(pconf.getProperty("oms.cloumnurl"));

            if (resultStr.equals("")) {
                return -1;
            }

            JSONObject resultobj = JSONObject.parseObject(resultStr);

            int code = resultobj.getInteger("code");

            if (code != 0) {
                // 查询接口出现异常,返回状态,使其自己查询
                return -1;
            }
            JSONArray tempArr = resultobj.getJSONArray("data");
            // 循环遍历  拆分 添加到两组 map
            for (Object o : tempArr
                    ) {
                // 遍历取出相应字段,加入 map
                JSONObject tempObj = JSONObject.parseObject(o.toString());
                String tableName = tempObj.getString("tablename");

                if (tableName.equals(pconf.getProperty("ons.qtytable"))){
                   // 此时是库存异动表
                    LoadCloumnsConfig.qutycolsmap.put(tableName,tempObj.getString("insertcols"));

                }else if (tableName.equals(pconf.getProperty("ons.menbertable"))){
                    // 此时是淘宝会员表
                    LoadCloumnsConfig.menbercolsmap.put(tableName,tempObj.getString("insertcols"));

                }else if (tableName.equals(pconf.getProperty("ons.qtyerrtable"))){
                    LoadCloumnsConfig.qtyerrmsp.put(tableName,tempObj.getString("insertcols"));

                } else if (tableName.equals(pconf.getProperty("ons.ordstatustable"))){
                    // 此时是同步订单状态表
                    LoadCloumnsConfig.ordstatusmap.put(tableName,tempObj.getString("insertcols"));

                }


                String insertCols = tempObj.getString("insertcols");
                String updateCols = tempObj.getString("updatecols");

                LoadCloumnsConfig.insertcolsmap.put(tableName, insertCols);
                LoadCloumnsConfig.updatecolsmap.put(tableName, updateCols);
            }

        } catch (Exception e) {
            log.error("调用查询表字段接口失败");
            return -1;
        }
        return 0;
    }

//    // 查询所有需要维护的各表字段
//    @TargetDataSource(name = "ds1")
//    public void queryCloumns() {
//
//        String sql = "select * from systablecolumn ";
//        JSONArray resultArr = QueryEngine.getInstance().doQueryObjectArray(sql, new Object[]{});
//
//        for (Object obj : resultArr
//                ) {
//            JSONObject temObj = JSONObject.parseObject(obj.toString());
//            String tableName = temObj.getString("tablename");
//            String insertCols = temObj.getString("insertcols");
//            String updateCols = temObj.getString("updatecols");
//
//            LoadCloumnsConfig.insertcolsmap.put(tableName, insertCols);
//            LoadCloumnsConfig.updatecolsmap.put(tableName, updateCols);
//
//        }
//
//
//    }

}
